<script setup>
import {onMounted, ref} from 'vue'
import AddPost from './componets/add-post.vue'
import {getPostTable,delPost} from '@/api/post'
// 弹窗
const showDialog = ref(false)
const changeDialog = () => {
  showDialog.value=true
}
// 删除弹窗
const centerDialogVisible = ref(false)
const delList = () => {
  centerDialogVisible.value = false
  getPostTableAPI()
}
// 分页
const handleSizeChange =async (val) => {
  paramspage.value.page = val
  await getPostTableAPI()
}
const handleCurrentChange =async (value) => {
  paramspage.value.size = value
  paramspage.value.page=1
 await getPostTableAPI()
}
const paramspage = ref({
  page: 1,
  size: 10,
  name: '',
  postName: '',
  status: null,
  total:''
})
// 表单数据
const tableData =ref([]) 
const getPostTableAPI =async () => {
  const {data,total} =await getPostTable(paramspage.value)
  tableData.value = data
  paramspage.value.total = total

}
onMounted(() => {
  getPostTableAPI()
})
const addRow = ref(null)
const edit = (row) => {
  showDialog.value = true
  addRow.value.getEditAPI(row)
}
const del = async (id) => {
  centerDialogVisible.value = true
  await delPost(id)
}
</script>

<template>
 <el-row>
  <el-form :inline="true" :model="paramspage" class="demo-form-inline">
    <el-form-item label="岗位编码：">
      <el-input clearable v-model="paramspage.postName" placeholder="请输入编码" />
    </el-form-item>
    <el-form-item label="岗位名称：">
      <el-input clearable v-model="paramspage.name" placeholder="请输入岗位名称" />
    </el-form-item>
    <el-form-item label="岗位状态：">
      <el-input clearable v-model="paramspage.status" placeholder="选择状态" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="getPostTableAPI">搜索</el-button>
    </el-form-item>
  </el-form>
 </el-row>
 <el-row type="flex" justify="end" style="margin-bottom:10px">
  <el-button type="primary" @click="changeDialog">添加</el-button>
 </el-row>
  <el-table :data="tableData" stripe style="width: 100%">
    <el-table-column align="center" type="index" label="序号" width="100" />
    <el-table-column align="center" prop="postName" label="岗位编码" width="100" />
    <el-table-column align="center" prop="name" label="岗位名称" />
    <el-table-column align="center" prop="display" label="显示顺序" />
    <el-table-column align="center" prop="status" label="状态" >
      <template v-slot="{row}">
      {{ row.status===1?'启用':'禁用' }}
      </template>
    </el-table-column>
    <el-table-column align="center" prop="createTime" label="创建时间" ></el-table-column>
    <el-table-column align="center" prop="address" label="操作" >
    <template v-slot="{row}">
      <el-button type="text" @click="edit(row)">修改</el-button>
      <el-button type="text" @click="del(row.id)">删除</el-button>
    </template>
    </el-table-column>
  </el-table>
<!-- 分页 -->
  <el-row style="margin-top:10px" type="flex" justify="center">
    <el-pagination
      :page-sizes="[5, 10, 15, 20]"
      :current-page="paramspage.page"
      layout="total, sizes, prev, pager, next, jumper"
      :total="paramspage.total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      style="height:30px"
    />
  </el-row>

 <add-post v-model:showDialog="showDialog" @addSucess="getPostTableAPI" ref="addRow"></add-post>
<!-- 删除弹窗 -->
<el-dialog v-model="centerDialogVisible" title="确定要删除吗？" width="30%" center>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="delList()">取消</el-button>
        <el-button type="primary" @click="delList()">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>
<style scoped>
/* ::v-deep .el-row{
  height: 30px;
} */
</style>